package com.baidu.controller;

import com.baidu.pojo.Exhibition;
import com.baidu.service.ExhibitionService;
import com.baidu.service.TicketService;
import com.baidu.utils.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/AdminExhibition")
public class AdminExhibitionController {

    @Autowired
    private ExhibitionService ordersService;
    @Autowired
    private TicketService ticketService;
    //分页查询所有展会信息
    @RequestMapping("/getAllExhibition")
    public String getAllExhibition(@RequestParam(defaultValue = "1") Integer currentPage,
                            @RequestParam(defaultValue = "3") Integer pageSize,
                            HttpServletRequest request,
                            HttpSession session){
        Pages pages=new Pages();
        pages.setCurrentPage(currentPage);
        pages.setPageSize(pageSize);
        int total=ordersService.count();
        pages.setTotalCount(total);
        request.setAttribute("pages",pages);
        List<Exhibition> list = ordersService.getByPage(pages.getCp(),pageSize);
        request.setAttribute("ex1",list);
        return "exhibitionList";
    }

    //添加展会
    @RequestMapping("/addExhibition")
    public String addExhibition(Exhibition exhibition,MultipartFile myFile)throws IOException {
        System.out.println("-----------------addExhibition---------------");
        System.out.println("orders:"+myFile);
        String fileName = myFile.getOriginalFilename();
//        ../../../web/img/upload
        File file = new File("D:/IdeaProject/cosProject/web/img/upload");
        System.out.println("file:"+file);
        System.out.println("目录"+file.exists());
        if (!file.exists()) {
            file.mkdirs();//创建目录
        }
        UUID uuid = UUID.randomUUID();
        System.out.println("uuid"+uuid);
        String realName = uuid+fileName;
        File realFile = new File(file,realName);
        myFile.transferTo(realFile);
        exhibition.setEpic("../img/upload/"+realName);
        ordersService.addExhibition(exhibition);
        return "redirect:/AdminExhibition/getAllExhibition";
    }

    //根据id删除
    @RequestMapping("/deletExhibitionById")
    public String deletExhibitionById(Integer eid){
        System.out.println("-------------deletExhibitionById-------------");
        System.out.println("eid:"+eid);

        //执行删除
        ordersService.deleteExhibition(eid);

        //重定向请求到/user/getAllUser
        return "redirect:/AdminExhibition/getAllExhibition";
    }

    //进入编辑页面
    @RequestMapping("/toEditExhibition")
    public String toEditExhibition(Integer eid,HttpServletRequest request){
        System.out.println("---------------toEditExhibition-------------------");
        System.out.println("id:"+eid);

        //根据id查询指定的用户信息
        Exhibition orders=ordersService.getExhibitionByTid(eid);
        System.out.println("orders"+orders);
        //将对象保存到request中
        request.setAttribute("ex2",orders);

        return "exhibitionUpdate";
    }

    //根据id修改指定的展会信息
    @RequestMapping("/updateExhibition")
    public String updateExhibition(Exhibition exhibition, MultipartFile myFile)throws IOException {
        System.out.println("------------updateExhibition--------------");
        System.out.println("exhibition:"+myFile.getOriginalFilename());
        String fileName = myFile.getOriginalFilename();
//        创建File对象,表示保存文件的目录 \\or/
        File file = new File("D:/IdeaProject/cosProject/web/img/upload");
        System.out.println("目录"+file.exists());
        if (!file.exists()) {
            file.mkdirs();//创建目录
        }
        UUID uuid = UUID.randomUUID();
        System.out.println("uuid"+uuid);
        String realName = uuid+fileName;
//      -->文件夹
        File realFile = new File(file,realName);
        myFile.transferTo(realFile);
        exhibition.setEpic("../img/upload/"+realName);
        //执行修改
        ordersService.updateExhibitionByOid(exhibition);

        return "redirect:/AdminExhibition/getAllExhibition";
    }
}
